/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.incremental;

import java.util.Collection;
import javax.swing.JOptionPane;
import syntax.ClockConditionAtom;
import syntax.Location;
import syntax.Message;
import syntax.Repository;
import syntax.Transition;

/**
 *
 * @author ennouaary
 */
public class JLocationDialogue extends javax.swing.JDialog {

    Repository repository;
    //Transition transition;
    TransitionObservable observable;
    Location location;
    Collection<Location> locations;
    boolean newNotEdit = true;

    /**
     * Creates new form JMessageFrame
     */
    public JLocationDialogue(TransitionObservable observable, Collection<Location> locations) {
        this.observable = observable;
        //this.transition = observable.getFrame().getTransition();
        location = new Location(null);
        this.observable = observable;
        this.locations = locations;
        newNotEdit = true;
        initComponents();
        fillInComponents();
        jActionLabel.setText("New Location");
    }

    public JLocationDialogue(Location location, TransitionObservable observable, Collection<Location> locations) {
        this.observable = observable;
        this.location = location;
        //this.transition = observable.getFrame().getTransition();
        location = new Location(null);
        this.observable = observable;
        this.locations = locations;
        newNotEdit = false;
        initComponents();
        fillInComponents();
        jActionLabel.setText("Edit Location");
    }

    private void fillInComponents() {
        jNameTextField.setText(location.getName());
    }

    private String getSymbol() {
        return jNameTextField.getText();
    }

    private boolean isNameDuplicated(String name) {
        boolean res = false;
        if(name.compareTo(location.getName())==0) {
            return false;
        }
        for (Location location : locations) {
            if (location.getName().compareTo(name) == 0) {
                res = true;
                break;
            }
        }
        return res;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jMessageTypeButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jOkButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jResetButton = new javax.swing.JButton();
        jActionLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jNameTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setTitle("Symbol");
        setAlwaysOnTop(true);
        setModal(true);
        getContentPane().setLayout(new java.awt.FlowLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jOkButton.setText("OK");
        jOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOkButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jOkButton);

        jCancelButton.setText("Cancel");
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jCancelButton);

        jResetButton.setText("Reset");
        jResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jResetButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jResetButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jPanel2, gridBagConstraints);

        jActionLabel.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jActionLabel.setText("New Location");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jActionLabel, gridBagConstraints);

        jLabel2.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jLabel2, gridBagConstraints);

        jNameTextField.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jNameTextField, gridBagConstraints);

        jPanel3.setPreferredSize(new java.awt.Dimension(30, 30));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jPanel3, gridBagConstraints);

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOkButtonActionPerformed
        // TODO add your handling code here:
        String symbol = getSymbol();
        if (symbol == null || symbol.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Location Symbol Required", "Location Symbol Required", JOptionPane.ERROR_MESSAGE);
            return;
        }
//        if (!(repository.getMessage(symbol) == null)) {
        if (isNameDuplicated(symbol)) {
            JOptionPane.showMessageDialog(this, "Location Symbol Duplicated", "Location Symbol Duplicated", JOptionPane.ERROR_MESSAGE);
            return;
        } else {
            // add the new symbol to repository
            location.setName(symbol);
            if (newNotEdit) {
                //transition.setDestLocation(location);
                locations.add(location);
                JOptionPane.showMessageDialog(this, "Transition Dest Location Changed");
            } else {
                JOptionPane.showMessageDialog(this, "Transition Dest Location Name Changed");
            }
            observable.setResult(location);
            observable.notifyTransitionChange();
            dispose();
        }
    }//GEN-LAST:event_jOkButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed

    private void jResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jResetButtonActionPerformed
        // TODO add your handling code here:
        reset();
    }//GEN-LAST:event_jResetButtonActionPerformed

    private void reset() {
        jNameTextField.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JLocationDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JLocationDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JLocationDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JLocationDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JLocationDialogue(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jActionLabel;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.ButtonGroup jMessageTypeButtonGroup;
    private javax.swing.JTextField jNameTextField;
    private javax.swing.JButton jOkButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jResetButton;
    // End of variables declaration//GEN-END:variables
}
